package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.reggie.common.R;
import com.example.reggie.entity.*;
import com.example.reggie.service.CategoryService;
import com.example.reggie.service.DishService;
import com.example.reggie.service.OrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/statics")
public class StaticsController {

    /*根据订单表来做菜系营收占比分析*/
    @Autowired
    private DishService dish;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private OrderDetailService orderDetailService;

    @GetMapping("/dishCash")
    public R dishCash(){
        /*这是查询到订单表中的数据*/
        List<OrderDetail> orderDetails = orderDetailService.list();



        ArrayList<Statics> staticsList = new ArrayList<>();
        for (OrderDetail orderDetail : orderDetails) {

            LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();

            Statics statics = new Statics();

            Long id = orderDetail.getDishId();
            Dish one = dish.getById(id);

            categoryLambdaQueryWrapper.eq(Category::getId,one.getCategoryId()).eq(Category::getType,1);


            Category one1 = categoryService.getOne(categoryLambdaQueryWrapper);
            boolean flag = false;

                if (staticsList.size()==0){
                    statics.setName(one1.getName());
                    statics.setValue(orderDetail.getAmount());
                    staticsList.add(statics);
                }else {
                    for (Statics statics1 : staticsList) {
                        if (statics1.getName().equals(one1.getName())){
                            statics1.setValue(statics1.getValue().add(orderDetail.getAmount()));
                            flag = true;
                            break;
                        }
                    }
                    if(!flag){
                        statics.setName(one1.getName());
                        statics.setValue(orderDetail.getAmount());
                        staticsList.add(statics);
                    }
                }
            }

        return R.success(staticsList);
    }
}
